package com.zry.fertilizer.presenter

import android.content.Intent
import android.view.View
import com.zry.fertilizer.R
import com.zry.fertilizer.adapter.BatchListAdapter
import com.zry.fertilizer.animation.RecyclerViewUtilKt
import com.zry.fertilizer.been.BatchInfoBean
import com.zry.fertilizer.been.ProductNameBean
import com.zry.fertilizer.been.ResultBean
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.activity.BatchCreateActivity
import com.zry.fertilizer.ui.activity.BatchInfoActivity
import com.zry.fertilizer.ui.activity.BatchListActivity
import com.zry.fertilizer.utils.ACacheUtil
import com.zry.fertilizer.utils.ToastUtil
import com.zry.fertilizer.view.BatchNamePopup
import com.zry.fertilizer.view.ProductNamePopup
import kotlinx.android.synthetic.main.activity_batch_list.*

class BatchListPresenter(private val activity: BatchListActivity) : ShowUserView {

    private var productNameSelect: ProductNameBean? = null
    private var batchInfoSelect: BatchInfoBean? = null

    private var batchList: MutableList<BatchInfoBean>? = null
    private var batchBeanCacheList: MutableList<BatchInfoBean>? = null
    private var batchListAdapter: BatchListAdapter? = null


    fun batchHandlerClick(view: View) {
        when (view.id) {
            R.id.productNameLinear -> {
                HttpServer(this).getProductByProductNameList(ACacheUtil().getLoginInfo().EntID, "", 1, 999, activity)
            }
            R.id.batchNameLinear -> {
                if (batchBeanCacheList!!.size>0){
                    BatchNamePopup(activity, activity.productNameLinear, batchBeanCacheList!!).setListener(object : BatchNamePopup.BatchNameListener {
                        override fun handleResult(batchInfoBean: BatchInfoBean) {
                            batchInfoSelect = batchInfoBean
                            activity.batchNameTV.text = batchInfoBean.BatchName
                            activity.productNameTV.text = "产品名称"
                            val batchBeanList = getSelectBatchName(batchInfoBean.BatchName)
                            batchList!!.removeAll(batchList!!)
                            batchList!!.addAll(batchBeanList)
                            batchListAdapter?.notifyDataSetChanged()
                        }
                    })
                }else{
                    ToastUtil.toast(activity,"暂时没有批次信息")
                }

            }
            R.id.createNewBatchBtn -> {
                val intent = Intent(activity, BatchCreateActivity::class.java)
                activity.startActivity(intent)

            }
        }
    }


    fun initRecycler() {
        batchList = mutableListOf()
        batchBeanCacheList= mutableListOf()
        val recyclerViewUtilKt = RecyclerViewUtilKt(activity, activity.batchListRecycler)
        recyclerViewUtilKt.initRecyclerView()
        batchListAdapter = BatchListAdapter(R.layout.item_batch_list, batchList!!, activity.title)
        recyclerViewUtilKt.setAdapter(batchListAdapter!!)
        batchListAdapter?.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(activity, BatchInfoActivity::class.java)
            intent.putExtra("title", activity.title)
            intent.putExtra("batchInfoBean", batchList!![position])
            activity.startActivity(intent)
        }
    }


    fun getBatchList() {
        HttpServer(this).getBatchAllList(ACacheUtil().getLoginInfo().EntID, "", "", 1, 999, activity)
    }


    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            27 -> {
                val resultData = data as ResultBean<MutableList<ProductNameBean>>
                if (resultData.State == "1") {
                    ProductNamePopup(activity, activity.productNameLinear, resultData.ResultData).setListener(object : ProductNamePopup.ProductNameListener {
                        override fun handleResult(productNameBean: ProductNameBean) {
                            productNameSelect = productNameBean
                            activity.productNameTV.text = productNameBean.ProductName
                            activity.batchNameTV.text = "批次名称"
                            val batchBeanList = getSelectProductName(productNameBean.ProductID)
                            batchList!!.removeAll(batchList!!)
                            batchList!!.addAll(batchBeanList)
                            batchListAdapter?.notifyDataSetChanged()
                        }
                    })
                } else {
                    ToastUtil.toast(activity, resultData.Message)
                }
            }
            28 -> {
                val resultData = data as ResultBean<MutableList<BatchInfoBean>>
                if (resultData.State == "1") {
                    batchBeanCacheList = resultData.ResultData
                    batchList!!.removeAll(batchList!!)
                    batchList!!.addAll(resultData.ResultData)
                    batchListAdapter?.notifyDataSetChanged()
                } else {
                    ToastUtil.toast(activity, resultData.Message)
                }
            }
        }
    }


    fun getSelectProductName(productID: String): MutableList<BatchInfoBean> {
        val batchBeanList: MutableList<BatchInfoBean> = mutableListOf()
        batchBeanCacheList!!.forEach {
            if (it.ProductID == productID) {
                batchBeanList.add(it)
            }
        }
        return batchBeanList
    }

    fun getSelectBatchName(batchName: String): MutableList<BatchInfoBean> {
        val batchBeanList: MutableList<BatchInfoBean> = mutableListOf()
        batchBeanCacheList!!.forEach {
            if (it.BatchName == batchName) {
                batchBeanList.add(it)
            }
        }
        return batchBeanList
    }
}